package gui;


import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
import app.Cookbook;
import app.Recipe;

/********************************************************
 *
 *  Project :  TeamProject
 *  Package :  gui
 *  File    :  ViewPanel.java
 *  Name    :  Tyler Daines
 *  Date    :  Nov 16, 2013
 *
 ********************************************************/
public class HomePanel extends JPanel implements ActionListener, MouseListener, KeyListener
{
	private JButton refreshButton;
	private JList<Object> searchResults, favorites;
	private Cookbook cookbook;
	private JLabel allLabel, favoriteLabel;

	/********************************************************
	 * Name : SearchPanel Purpose : Constructor Parameters :
	 ********************************************************/
	public HomePanel()
	{
		setLayout(new MigLayout());
		cookbook = new Cookbook();
		initButtons();
		initList();
		initLabels();
		
		addComponents();
	}


	/********************************************************
	 * Name : initButtons Purpose : Parameters : Returns : void
	 ********************************************************/
	private void initButtons()
	{

		refreshButton = new JButton("Refresh");
		refreshButton.addActionListener(this);

	}

	/********************************************************
	 * Name : initList Purpose : Parameters : Returns : void
	 ********************************************************/
	private void initList()
	{
		searchResults = new JList<>();
		searchResults.setMinimumSize(new Dimension(400, 500));
		searchResults.addMouseListener(this);
		searchResults.addKeyListener(this);
		
		favorites = new JList<>();
		favorites.setMinimumSize(new Dimension(400, 500));
		favorites.addMouseListener(this);
		favorites.addKeyListener(this);
		refreshLists();
	}
	
	/********************************************************
	* Name       : refreshLists
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	public void refreshLists()
	{
		ArrayList<Recipe> allRecipes = cookbook.viewAllRecipes();
		allRecipes = (ArrayList<Recipe>) sortList(allRecipes);
		
		ArrayList<Recipe> favoritesList = cookbook.searchByFavorite();
		favoritesList = (ArrayList<Recipe>) sortList(favoritesList);
		
		searchResults.setListData(allRecipes.toArray());
		favorites.setListData(favoritesList.toArray());
		validate();
		
	}


	/********************************************************
	* Name       : initLabels
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initLabels()
	{
		allLabel = new JLabel("All Recipes:");
		favoriteLabel = new JLabel("Favorites:");
		
	}

	/********************************************************
	 * Name : addComponents Purpose : Parameters : Returns : void
	 ********************************************************/
	private void addComponents()
	{

		add(allLabel, "cell 0 0");
		add(favoriteLabel, "cell 1 0");
		
		add(searchResults, "cell 0 1");
		add(favorites, "cell 1 1");
		
		add(refreshButton, "cell 0 2");

	}
	
	/********************************************************
	* Name       : sortList
	* Purpose    : 
	* Parameters : 
	* Returns    : List<Recipe>
	********************************************************/
	private List<Recipe> sortList(List<Recipe> recipes)
	{

		Collections.sort(recipes);
		
		return recipes;
	}
	
	
	@Override
	public String getName()
	{
		return "Home";
	}
	
	public JList<Object> getSearchResults()
	{
		return searchResults;
	}
	

	public void setSearchResults(JList<Object> searchResults)
	{
		this.searchResults = searchResults;
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent e)
	{
		if(e.getClickCount() == 2)
		{
			if(e.getSource() == searchResults)
			{
				if(searchResults.getSelectedValue() instanceof Recipe)
				{
					Gui.createTab(new RecipePanel((Recipe) searchResults.getSelectedValue()));
				}
			}

			if(e.getSource() == favorites)
			{
				if(favorites.getSelectedValue() instanceof Recipe)
				{
					Gui.createTab(new RecipePanel((Recipe) favorites.getSelectedValue()));
				}
			}

			
		}
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == refreshButton)
		{
			refreshLists();
		}
		
	}


	/**
	* Name :keyPressed
	*@see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
	**/
	@Override
	public void keyPressed(KeyEvent e)
	{
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			if(e.getSource() == searchResults)
			{
				if(searchResults.getSelectedValue() instanceof Recipe)
				{
					Gui.createTab(new RecipePanel((Recipe) searchResults.getSelectedValue()));
				}
			}

			if(e.getSource() == favorites)
			{
				if(favorites.getSelectedValue() instanceof Recipe)
				{
					Gui.createTab(new RecipePanel((Recipe) favorites.getSelectedValue()));
				}
			}
		}
		
		
	}


	/**
	* Name :keyReleased
	*@see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
	**/
	@Override
	public void keyReleased(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}


	/**
	* Name :keyTyped
	*@see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
	**/
	@Override
	public void keyTyped(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}
}

